package indi.zhifa.engine.starter.redis;

import com.alibaba.fastjson2.support.spring6.data.redis.FastJsonRedisSerializer;
import indi.zhifa.engine.redis.aop.CacheAop;
import indi.zhifa.engine.redis.properties.CacheRedisProperties;
import indi.zhifa.engine.redis.properties.RedisConfig;
import indi.zhifa.engine.redis.properties.RedissonProperties;
import indi.zhifa.engine.redis.utils.RedisTemplateProvider;
import lombok.RequiredArgsConstructor;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@RequiredArgsConstructor
@AutoConfiguration
@ConditionalOnProperty(prefix = "zhifa.redis.cache",name = "enabled", havingValue = "true")
public class CacheRedisAutoConfiguration {

    @ConfigurationProperties(prefix = "zhifa.redis.cache")
    @Bean(name = "cache-redis-properties")
    public CacheRedisProperties redisProperties(){
        return new CacheRedisProperties();
    }

    /**
     * springBoot的配置很复杂， 配一个简单的，如果需要大家自行配一个
     *
     */
    @Bean(name = "cache-redis-connection-factory")
    public RedisConnectionFactory redisConnectionFactory(@Qualifier(value = "cache-redis-properties") CacheRedisProperties pRedisProperties){
        return RedisConfig.redisConnectionFactory(pRedisProperties);
    }


    @Bean(name = "cache-redis-template")
    public RedisTemplate<String, Object> redisTemplate(@Qualifier(value = "cache-redis-connection-factory") RedisConnectionFactory factory,
                                                       @Qualifier(value = "default-redis-json-serializer") FastJsonRedisSerializer pFastJsonRedisSerializer) {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
        redisTemplate.setConnectionFactory(factory);
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(pFastJsonRedisSerializer);
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(pFastJsonRedisSerializer);
        return redisTemplate;
    }


    @ConditionalOnProperty(prefix = "zhifa.redis",name = {"enabled","redisson.enabled"}, havingValue = "true", matchIfMissing = false)
    @Bean
    public CacheAop cacheAop(@Qualifier(value = "redisson-redis-properties") RedissonProperties pRedisProperties,
                             RedisTemplateProvider pRedisTemplateProvider,
                             RedissonClient pRedissonClient){
        return new CacheAop(pRedisTemplateProvider,pRedissonClient,pRedisProperties);
    }


}
